package cn.tedu.net.collection.file;

import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils;

import java.io.*;

//本类用于测试文件的读取
public class Test4_InputStream {
    public static void main(String[] args) {
        method();
        method2();

    }

    public static void method2() {
        InputStream in=null;
        try {
            //创建高效字节流读取对象
            in =new BufferedInputStream(new FileInputStream(new File("D:\\ready\\4.txt")));
            int b;
            while ((b=in.read())!=-1){
                System.out.println(b);
            }
        } catch (IOException e) {
            e.printStackTrace();//打印错误信息
        }finally{//try--catch--finally一定会执行到的代码块 常用于关流
            //释放资源
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void method(){
        //创建字节流读取的对象
    InputStream in=null;
    try {
         in=new FileInputStream("D:\\ready\\4.txt");
        //包证此路径下的文件是存在且有内容的
        //开始读取,每次读取一个字节 exlipce返回-1
        /*System.out.println(in.read());//97
        System.out.println(in.read());//98
        System.out.println(in.read());//99
        System.out.println(in.read());//13*/
        int b;
        while ((b=in.read())!=-1){
            System.out.println(b);
        }
    } catch (IOException e) {
        e.printStackTrace();//打印错误信息
    }finally{//try--catch--finally一定会执行到的代码块 常用于关流
        //释放资源
        try {
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
};
}
